<template>
  <div>
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" title="应用字段维护" @del="del" @newCreate="newCreate"></v-tableTitle>
    <GTable ref="table" :hasCheck="true" :hasEdit="true" api="/v1/plApplicationField/selectPlApplicationFieldList" :columnSource="baseColumns" :cbFun="cbFun"
      @selection-change="data => tableSelectData = data" @setdialog="setdialog" />

    <el-dialog :title="dialogModule.title" :visible.sync="dialogModule.flag" center v-dialogDrag width="720px" @closed="closed">
      <div v-tit="'应用字段维护'">
        <GForm ref="detail" :config="formConfig" :rules="formRules" labelWidth="120px" v-model="form"></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import tableTitle from 'commons/TableTitle'

export default {
  name: 'appInfo',
  data() {
    return {
      tableSelectData: [],
      dialogModule: {
        loading: false,
        flag: false,
        title: '',
        data: null,
        type: ''
      },
      form: {
        applicationNo: null,
        applicationName: null,
        fieldNo: '',
        fieldName: '',
        fieldDataType: '',
        fieldType: '',
        fieldLength: '',
        fieldDesc: ''
      },
      options: {
        app: [],
        type: [],
        dataType: []
      }
    }
  },
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t('common.create'),
          invalid: 'newCreate'
        },
        {
          label: this.$t('common.delete'),
          disabled: true,
          invalid: 'del'
        }
      ]
    },
    baseColumns() {
      return [
        {
          label: '应用编号',
          prop: 'applicationNo',
          input: 'input'
        },
        {
          label: '应用名称',
          prop: 'applicationName',
          input: 'input'
        },
        {
          label: '字段Code',
          prop: 'fieldNo',
          input: 'input'
        },
        {
          label: '字段名称',
          prop: 'fieldName',
          input: 'input'
        },
        {
          label: '字段数据类型',
          prop: 'fieldDataType',
          input: 'select',
          options: this.options.dataType
        },
        {
          label: '字段类型',
          prop: 'fieldType',
          input: 'select',
          options: this.options.type
        },
        {
          label: '字段长度',
          prop: 'fieldLength',
          input: 'input'
        },
        {
          label: '字段描述',
          prop: 'fieldDesc'
        },
        {
          label: this.$t('app.addName'),
          prop: 'insUser'
        },
        {
          label: this.$t('app.addDate'),
          prop: 'insTime'
        },
        {
          label: this.$t('app.updateName'),
          prop: 'updUser'
        },
        {
          label: this.$t('app.updateDate'),
          prop: 'updTime'
        }
      ]
    },
    formConfig() {
      return [
        {
          prop: 'applicationId',
          type: 'select',
          label: '所属应用',
          options: this.options.app
        },
        {
          prop: 'fieldNo',
          type: 'input',
          label: '字段Code',
          desc:
            '填写对象属性字段，同一应用不能重复，比如: DeptName、ApplicationName'
        },
        {
          prop: 'fieldName',
          type: 'input',
          label: '字段名称',
          desc: '填写对象属性名称，同一应用不能重复，比如: 部门名称、应用名称'
        },
        {
          prop: 'fieldDataType',
          type: 'select',
          label: '字段数据类型',
          options: this.options.dataType,
          desc: '选择字段类型，数据字典类型的字段需要转换'
        },
        {
          prop: 'fieldType',
          type: 'select',
          label: '字段类型',
          options: this.options.type
        },
        {
          prop: 'fieldLength',
          type: 'number',
          label: '字段长度',
          disabled: true
        },
        {
          prop: 'fieldDesc',
          type: 'textarea',
          label: '字段描述'
        }
      ]
    },
    formRules() {
      return {
        applicationId: [
          {
            required: true,
            message: this.$inputPlease('cn.appCode'),
            trigger: 'change'
          }
        ],
        fieldNo: [
          {
            required: true,
            message: '请输入字段Code',
            trigger: 'blur'
          }
        ],
        fieldName: [
          {
            required: true,
            message: '请输入字段名称',
            trigger: 'blur'
          }
        ],
        fieldDataType: [
          {
            required: true,
            message: '请选择字段数据类型',
            trigger: 'change'
          }
        ],
        fieldType: [
          {
            required: true,
            message: '请选择字段类型',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    handleConfirm() {
      this.$refs.detail.validate().then(valid => {
        const params = this._.cloneDeep(this.form)
        const appObj = this.options.app.find(
          opt => opt.id == params.applicationId
        )
        params.applicationNo = appObj.applicationNo
        params.applicationName = appObj.applicationName
        let promise
        if (this.dialogModule.type == 'edit') {
          promise = this.$post(
            `/v1/plApplicationField/editPlApplicationField`,
            params
          )
        } else {
          promise = this.$post(
            `/v1/plApplicationField/addPlApplicationField`,
            params
          )
        }
        promise.then(res => {
          this.$message.success(res.returnMgr)
          this.getTableData()
          this.$refs.detail.resetFields()
          this.dialogModule.flag = false
        })
      })
    },
    getInfoById(id) {
      this.$post('/v1/plApplicationField/selectPlApplicationFieldById', {
        id
      }).then(res => {
        this.$refs.detail?.resetFields()
        this.form = res.data.item
        this.dialogModule.flag = true
      })
    },
    setdialog(data) {
      this.dialogModule.title = `编辑[${data.applicationName}]应用字段`
      this.dialogModule.type = 'edit'
      this.dialogModule.data = data
      this.getInfoById(data.id)
    },
    newCreate() {
      this.dialogModule.title = '新增应用字段'
      this.dialogModule.type = 'add'
      this.dialogModule.data = null
      this.$refs.detail?.resetFields()
      this.dialogModule.flag = true
    },
    //删除
    async del(data) {
      this.$post('/v1/plApplicationField/deletePlApplicationFieldByIds',data).then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
      })
    },
    getOptions() {
      this.$post('/v1/plApplication/selectPlApplicationList', {
        pageNum: 1,
        pageSize: 999,
        applicationStatus: '50301'
      }).then(res => {
        const data = res.data.page.records
        this.options.app = data.map(i => {
          i.label = i.applicationName
          i.value = i.id
          return i
        })
      })
      this.$get('/system/codeLookup/FieldDataType').then(res => {
        this.options.dataType = res.data.item.map(i => {
          i.label = i.description
          i.value = i.code
          return i
        })
      })
      this.$get('/system/codeLookup/FieldType').then(res => {
        this.options.type = res.data.item.map(i => {
          i.label = i.description
          i.value = i.code
          return i
        })
      })
    },
    closed(){
      this.$refs.detail?.resetFields()
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total }
    },
    async getTableData() {
      this.$refs.table.getTableData()
    }
    //设置弹框
  },
  mounted() {
    this.getOptions()
  },
  components: {
    'v-tableTitle': tableTitle
  }
}
</script>

<style scoped>
</style>
